#!/bin/bash

###
#
#   Mailx for Gmail Command Line for Ubuntu
#   Version 1.0
#
#   USAGE:
#       sudo ./setup_gmail_command_line.sh install
#
#
###

#
# Editable Settings
#
GMAIL_USER='YOUR_GMAIL_USER_NAME'
GMAIL_PASS='YOUR_GMAIL_USER_PASSWORD'


#
# Non-Editable Settings
#
SCRIPT=$(readlink -f $0)
SCRIPTDIR=`dirname $SCRIPT`
ARGS=("$@")
ARG_COUNT="$#"


#
# main functions
#
function parse_commandline() {
    echo "parsing command line: ${ARG_COUNT} parameters"

    if [ "${ARGS[0]}" == "install"  ]; then
        return
    else
        echo "to run: $SCRIPT install"
        exit 1
    fi
}

function preamble() {
    cat <<EOB

*** Ubuntu Gmail Command Line Script ***
*** http://klenwell.com/is/UbuntuCommandLineGmail ***

SETTINGS
script       : $SCRIPT
gmail user   : $GMAIL_USER
gmail pass   : $GMAIL_PASS

NOTES
You can edit the user and password settings by editing
this script.  If you do not do this, you will need to
edit the two config files at the end of the script.
Instructions will be provided when the script at the end.

You will be asked for your sudo password.  This is to
install the necessary packages.

EOB
    prompt_
}

function install_mail_packages() {
    sudo apt-get install msmtp heirloom-mailx
}

function install_certificate() {
    sudo apt-get install ca-certificates
    sudo update-ca-certificates
}

function configure_msmtp() {
    cat <<EOF > ~/.msmtprc
# config options: http://msmtp.sourceforge.net/doc/msmtp.html
defaults
logfile /tmp/msmtp.log

# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
user $GMAIL_USER@gmail.com
password $GMAIL_PASS
from $GMAIL_USER@gmail.com
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt

# set default account to use (not necessary with single account)
account default : gmail
EOF

    chmod 600 ~/.msmtprc
}

function configure_mailx() {
    cat <<EOF > ~/.mailrc
# set smtp for mailx

# gmail account (default)
# $ mailx -s "subject line" -a /path/attachment recipient@email.com < /path/body.txt
set from="$GMAIL_USER@gmail.com ($GMAIL_USER)"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"
EOF
}

function afterword() {

    cat <<EOB

-- The necessary packages and files have now been installed.

EOB

    if [ "$GMAIL_USER" == 'YOUR_GMAIL_USER_NAME' ]; then
        cat <<EOB

NOTE: You still need to complete the following steps:

    1. Set your email address and password in the
       file ~/.msmtprc (replace CAPS):

        $ gedit ~/.msmtprc

    2. Set your your user name in the file
       ~/.mailrc (replace CAPS):

        $ gedit ~/.mailrc

    3. Send a test email:

        $ echo -e "testing email from the command line" > /tmp/test_email
        $ mailx -s "mailx gmail test" YOURNAME@gmail.com < /tmp/test_email
        $ tail /tmp/msmtp.log

    4. Check your email to confirm that you received it.

EOB
    else
        # send test email
        echo "sending test email now"
        mailx -s "mailx gmail test" $GMAIL_USER@gmail.com < /tmp/test_email
        tail /tmp/msmtp.log

        cat <<EOB

A test email has been sent to $GMAIL_USER.  If successful, you
can login to your account now and find it.  You can also check
the mail log:

    $ tail /tmp/msmtp.log

EOB
    fi
}


#
# support functions
#
function prompt_() {
    read -s -n1 -p "Hit [ENTER] or [SPACE] to continue, any other key to quit " INPUT_
    if [ ! -e $INPUT_ ]; then
        echo -e "\nquitting now...\n"
        exit 1
    fi
    echo -e "\n"
}


#
# main
#
parse_commandline
preamble
install_mail_packages
install_certificate
configure_msmtp
configure_mailx
afterword
